import {supportFunction} from "../../utils/supportFunction";

class ModuleInstanceManager {
    constructor() {
        if(!ModuleInstanceManager.instance) {
            this.instances = {};
            ModuleInstanceManager.instance = this;
        }

        return ModuleInstanceManager.instance;
    }

    init(key) {
        if(!supportFunction.hasProp(this.instances, key)) {
            this.instances[key] = {};
        }
    }

    get(key, type = undefined) {
        if(!supportFunction.hasProp(this.instances, key)) {
            throw Error('No specified key instance found');
        }
        if(type) {
            if(!supportFunction.hasProp(this.instances[key], type)) {
                throw Error('No specified type instance found');
            }
            return supportFunction.getOwn(this.instances[key], type);
        }
        return supportFunction.getOwn(this.instances, key);
    }

    getModuleInstance(key, type, instanceKey) {

    }

    add(key, type, instance) {
        if(!supportFunction.hasProp(this.instances, key)) {
            this.instances[key] = {};
        }
        if(!this.instances[key][type]) {
            this.instances[key][type] = [];
        }
        this.instances[key][type].push(instance);
    }

    remove(key) {
        if(supportFunction.hasProp(this.instances, key)) {
            for(let type in this.instances[key]) {
                this.instances[key][type] = [];
            }
        }
    }

    clear() {
        for(let key in this.instances) {
            this.remove(key);
        }
    }
}

const moduleInstanceManager = new ModuleInstanceManager();
export default moduleInstanceManager;
